package com.sikadwafastdaisfondturbo.pretloan.analysis

import com.sikadwa.analysis.AnalysisBuilder
import com.sikadwa.analysis.AnalyticsUtil
import com.sikadwa.syc.SikadwaSysBuilder
import com.sikadwa.syc.SikadwaSysDataHelper
import com.sikadwafastdaisfondturbo.pretloan.BuildConfig
import com.sikadwafastdaisfondturbo.pretloan.app.App
import com.sikadwafastdaisfondturbo.pretloan.base.BaseApp
import com.sikadwafastdaisfondturbo.pretloan.utils.config.Configs
import com.sikadwafastdaisfondturbo.pretloan.utils.http.AppUrls
import com.sikadwafastdaisfondturbo.pretloan.utils.http.AppUrls.addCommonParams
import com.sikadwafastdaisfondturbo.pretloan.utils.http.HttpHelper
import com.sikadwafastdaisfondturbo.pretloan.utils.http.IHttpCallback
import org.json.JSONObject


/**
 * TODO: Analysis Lib 初始化时机
 */
object TestAnalysisInit {

    /**
     * TODO: APP启动时，初始化一下 analysis lib
     */
    fun initAnalysisLibAtAppLaunch() {
        AnalysisBuilder.product = Configs.AppName
        AnalysisBuilder.appVersion = BuildConfig.VERSION_CODE.toString()
        AnalysisBuilder.appPckName = BuildConfig.APPLICATION_ID

        AnalysisBuilder.aesKey = "IWyKojGMVFszL5f2"
        AnalysisBuilder.aesIv = "4WaIMtGiLY9cDwWl"

        AnalysisBuilder.host = AppUrls.APP_URL
        AnalysisBuilder.zone = ""
        AnalysisBuilder.pathCommon = "/yQ4tnes08gUZz7zBwXLFw"

        AnalysisBuilder.pathGoogleInstall = "/R8g2gNq8L5Zc3jhfSwwIzQ"
        AnalysisBuilder.pathAdjustInstall = "/W9iowaSuGdGOuPY0ce8mA"
        AnalysisBuilder.pathFacebookInstall = "/slNo2wMW0vz1zpf2s9Gg"
        AnalysisBuilder.pathFirebaseInstall = "/AIh3MhBW0mVgO4VXzF4Nuw"

        AnalyticsUtil.init(BaseApp.application)
    }

    /**
     * TODO: 成功获取地区转发路径时，初始化一下 analysis lib
     */
    fun initAnalysisLibAtZoneSuccess() {
        AnalysisBuilder.host = AppUrls.APP_URL
        AnalysisBuilder.zone = Configs.getSPString(Configs.SPKEY.SIKADWA_TIMEZONE)
        AnalyticsUtil.init(BaseApp.application)
    }

    /**
     * TODO: 成功登录时，初始化一下 analysis lib
     */
    fun initAnalysisLibAtLoginSuccess() {
        AnalysisBuilder.token = Configs.getSPString(Configs.SPKEY.SIKADWA_TOKEN)
        AnalysisBuilder.userId = Configs.getSPString(Configs.SPKEY.SIKADWA_USERID)
        AnalyticsUtil.init(BaseApp.application)
    }
}

/**
 * TODO: Analysis Lib 打点时机
 */
object TestAnalysisAddEvent {

    /**
     * TODO: 在调用登录接口，并成功返回时，如果 type=="REGISTER"，则调用注册事件打点
     */
    fun addRegisterEvent() {
        AnalyticsUtil.addRegisterEvent()
    }

    /**
     * TODO: 在调用提交贷款申请接口，并成功返回时，则调用申请事件打点
     */
    fun addApplyEvent() {
        AnalyticsUtil.addApplyEvent()
    }
}

/**
 * TODO: Data Lib 调用时机
 */
object TestDataLibInvoke {

    /**
     * TODO: 在首页每次点击申请时（在已登录的前提下），调用一下 data lib 的上报
     * 获取设备数据上传状态
     */
    fun loadDInfoTypeAction(){
        val params = JSONObject().addCommonParams().apply {
            put("path", AppUrls.PATH.DEVICE_INFO_TYPES)
        }
        val httpUrl = AppUrls.getHttpUrl(AppUrls.URL.COMMON)
        HttpHelper.post(httpUrl,params,object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                when(data.optInt("code")) {
                    0 -> {//成功
                        val obj = data.optJSONObject("obj")
                        obj?.let {
                            SikadwaSysBuilder.httpHost = AppUrls.APP_URL
                            SikadwaSysBuilder.httpTimeZone = Configs.getSPString(Configs.SPKEY.SIKADWA_TIMEZONE)
                            SikadwaSysBuilder.userToken = Configs.getSPString(Configs.SPKEY.SIKADWA_TOKEN)
                            SikadwaSysBuilder.configData = Configs.getSPString(Configs.SPKEY.SIKADWA_COMMON)

                            val deviceInfoTypes = obj.optString("deviceInfoTypes")
                            SikadwaSysBuilder.diceReportTypes = deviceInfoTypes
                            SikadwaSysDataHelper.onCheckSubmitDataByTypes(App.instance())
                        }
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
            }

            override fun onRequestFinish() {
            }
        })
    }
}